package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.sc.nft.dao.*;
import com.sc.nft.entity.UserCityServerCentre;
import com.sc.nft.entity.UserRelevance;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.UserRelevanceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class UserRelevanceServiceImpl implements UserRelevanceService {

    private final UserRelevanceDao userRelevanceDao;
    private final UserCityServerCentreDao userCityServerCentreDao;
    private final UserInfoDao userInfoDao;
    private final UserHnaExtDao userHnaExtDao;
    private final LlUserExtDao llUserExtDao;

    @Override
    public Boolean userInfoDataSynchronism(String recommendUserIdList, String phone, Long userId) {
        List<UserRelevance> userRelevances = new ArrayList<>();
        List<String> split = StrUtil.split(recommendUserIdList, ",");
        Collections.reverse(split);
        int i = 0;
        for (String s : split) {
            UserRelevance userRelevance = new UserRelevance();
            userRelevance.setUserId(Convert.toLong(s));
            userRelevance.setRelevanceId(userId);
            userRelevance.setLv(++i);
            userRelevance.setCreateTime(new Date());
            userRelevance.setUpdateTime(new Date());
            userRelevances.add(userRelevance);
        }
        userRelevanceDao.saveBatch(userRelevances);
        return Boolean.TRUE;
    }

    @Override
    public List<Long> userRelevanceByUserId(Long userId) {
        return userRelevanceDao.userRelevanceByUserId(userId);
    }


    @Override
    public Integer getUserFans() {
        return userRelevanceDao.getUserFans();
    }
    @Override
    public Boolean userIdAndRelevanceIdIsIdenticalDictyosome(Long userId, Long relevanceId) {
        // 上级必须是网体
        UserCityServerCentre mainBody = userCityServerCentreDao.getByUserId(userId);
        if (BeanUtil.isEmpty(mainBody)) {
            throw new GlobalRunTimeException("当前账号必须为城市服务中心");
        }
        // 下级必须是这个用户网体内的,并且必须要是没被截断的
        if (!userRelevanceDao.verifyThatTheUserIsInTheirOwnNetwork(userId, relevanceId)) {
            throw new GlobalRunTimeException("接收人必须在您的网体内");
        }

//        // 下级必须开通钱包
//        Boolean open = Boolean.FALSE;
//        // 杉德
//        UserInfo userInfo = userInfoDao.getById(relevanceId);
//        if (!open) {
//            if (BeanUtil.isNotEmpty(userInfo)) {
//                if (userInfo.getSandAccountOpen()) {
//                    open = Boolean.TRUE;
//                }
//            }
//        }
//        if (!open) {
//            LlUserExt llUserExt = llUserExtDao.getByUserId(relevanceId);
//            if (BeanUtil.isNotEmpty(llUserExt)) {
//                if (llUserExt.getStatus() == AccountStatusEnum.SUCCESS) {
//                    open = Boolean.TRUE;
//                }
//            }
//        }
//        if (!open) {
//            UserHnaExt userHnaExt = userHnaExtDao.getByUserId(relevanceId);
//            if (BeanUtil.isNotEmpty(userHnaExt)) {
//                if (userHnaExt.getStatus() == AccountStatusEnum.SUCCESS) {
//                    open = Boolean.TRUE;
//                }
//            }
//        }
//        if (!open) {
//            throw new GlobalRunTimeException("接收人必须开通钱包");
//        }
        return Boolean.TRUE;
    }

}
